package com.yekaile.carsystem.service;

import cn.hutool.core.date.DateUtil;
import com.yekaile.carsystem.controller.vo.MenuVO2;
import com.yekaile.carsystem.entity.*;
import com.yekaile.carsystem.mapper.*;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


/**
 * @author yekaile
 */
@Service
@AllArgsConstructor(onConstructor_=@Autowired)
public class AuthorityServiceImpl implements AuthorityService {


    UserMapper userMapper;


    SystemAuthorityMapper systemAuthorityMapper;


    UserRoleMapper userRoleMapper;

    UserAuthorityMapper userAuthorityMapper;


    RoleAuthorityMapper roleAuthorityMapper;



    @Override
    public int distributeRoleAuthority(Integer roleId, List<Integer> systemAuthortyIdList) {
        List<RoleAuthority> roleAuthorityList = new ArrayList<>(systemAuthortyIdList.size());
        systemAuthortyIdList.forEach(item -> {
            RoleAuthority roleAuthority = new RoleAuthority();
            roleAuthority.setRoleId(roleId);
            roleAuthority.setSystemAuthorityId(item);
            roleAuthority.setGmtCreated(DateUtil.now());
            roleAuthority.setGmtModified(DateUtil.now());
            roleAuthority.setIsDeleted(0);
        });

        return roleAuthorityMapper.batchInsert(roleAuthorityList);
    }

    @Override
    public int distributeOnlyUserAuthority(Integer userId, List<Integer> systemAuthortyIdList) {
        List<UserAuthority> userAuthorityList = new ArrayList<>(systemAuthortyIdList.size());
        systemAuthortyIdList.forEach(item -> {
            UserAuthority userAuthority = new UserAuthority();
            userAuthority.setUserId(userId);
            userAuthority.setSystemAuthorityId(item);
            userAuthority.setGmtCreated(DateUtil.now());
            userAuthority.setGmtModified(DateUtil.now());
            userAuthority.setIsDeleted(0);
        });

        return userAuthorityMapper.batchInsert(userAuthorityList);
    }

    @Override
    public int addRole(Integer userId, Integer roleId) {
        UserRole userRole = new UserRole();
        userRole.setRoleId(roleId);
        userRole.setUserId(userId);
        return userRoleMapper.insertSelective(userRole);
    }

    @Override
    public int deleteRole(Integer userId, Integer roleId) {
        UserRoleExample example = new UserRoleExample();
        UserRoleExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andRoleIdEqualTo(roleId);
        UserRole userRole = userRoleMapper.selectByExample(example).get(0);
        userRole.setIsDeleted(1);
        return userRoleMapper.updateByPrimaryKeySelective(userRole);
    }

    @Override
    public int updateRole(UserRole userRole, Integer roleId) {
        UserRoleExample example = new UserRoleExample();
        UserRoleExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userRole.getUserId());
        criteria.andRoleIdEqualTo(roleId);
        userRole = userRoleMapper.selectByExample(example).get(0);
        userRole.setRoleId(roleId);
        return userRoleMapper.updateByPrimaryKeySelective(userRole);
    }

    @Override
    public List<Role> getRoles(Integer userId) {
        return userMapper.selectRoles(userId);
    }

    @Override
    public int addAuthority(SystemAuthority authority) {
        return systemAuthorityMapper.insertSelective(authority);
    }

    @Override
    public List<SystemAuthority> getAllAuthorities() {
        return systemAuthorityMapper.selectAll();
    }

    @Override
    public Integer deleteAuthority(Integer authorityId) {
        SystemAuthority authority = systemAuthorityMapper.selectByPrimaryKey(authorityId);
        authority.setGmtModified(DateUtil.now());
        authority.setIsDeleted(1);
        return systemAuthorityMapper.updateByPrimaryKeySelective(authority);
    }

    @Override
    public Set<MenuVO2> getDistinctMenus() {
        return new HashSet<>(systemAuthorityMapper.selectDistinctMenuCodeAndMenuName());

    }


}
